﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;

namespace ESoft.Core.Utils.Extensions
{
    public static class ObjectExtensions
    {
        /// <summary>
        /// 判断是否为null,
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool IsNull(this object obj)
        {
            return obj == null;
        }

        /// <summary>
        /// 判断是否不为null
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns><c>true</c> if [is not null] [the specified source]; otherwise, <c>false</c>.</returns>
        public static bool IsNotNull(this object source)
        {
            return source != null;
        }

        /// <summary>
        /// 转换
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T As<T>(this object obj) where T : class
        {
            return obj as T;
        }

        public static bool Is<T>(this object obj)
        {
            return obj is T;
        }

        public static bool In<T>(this T obj, List<T> list)
        {
            if (list.IsNull() || list.Any().IsFalse())
            {
                return false;
            }
            return list.Contains(obj);
        }
        public static void SetModelValue<T>(this T model, PropertyInfo p, object value) where T : class, new()
        {
            if (p.IsNull())
            {
                return;
            }
            if (value.IsNull() || value.ToString().IsNullOrEmpty())
            {
                p.SetValue(model,value,null);
                return;
            }
            if (p.PropertyType.IsGenericType.IsFalse())
            {
                p.SetValue(model,Convert.ChangeType(value,p.PropertyType),null);
            }
            else
            {
                Type genericTypeDefinition = p.PropertyType.GetGenericTypeDefinition();
                if (genericTypeDefinition == typeof(Nullable<>))
                {
                    p.SetValue(model, Convert.ChangeType(value, Nullable.GetUnderlyingType(p.PropertyType)), null);

                }
            }
        }

        public static object GetModelPropertyValue<T>(this T model,string propertyName) where T : class, new()
        {
            var p = typeof(T).GetProperty(propertyName).GetValue(model, null);
            return p;
        }
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToJson(this object obj)
        {
            var json = JsonConvert.SerializeObject(obj);
            return json;
        }

        public static T ThrowIfNull<T>(this T @this, string message = "不能为空")
        {
            if (@this.IsNull())
            {
                throw new ArgumentNullException(message);
            }
            return @this;
        }
        public static T ThrowIfFalse<T>(this T @this,bool condition, string message = "请输入正确的条件")
        {
            if (condition.IsFalse())
            {
                throw new ArgumentNullException(message);
            }
            return @this;
        }
    }
}
